package io.oschina.binv.kapp.framework.extend

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.support.v4.content.FileProvider
import android.util.Log
import com.umeng.analytics.MobclickAgent
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder
import io.oschina.binv.kapp.BuildConfig
import java.io.File
import java.io.PrintWriter
import java.io.StringWriter
import android.R.attr.orientation
import android.provider.MediaStore
import android.provider.MediaStore.Images.ImageColumns.ORIENTATION
import java.net.URI
import java.lang.reflect.AccessibleObject.setAccessible




/**
 * Created by xuybin on 2017/8/31.
 */
fun Context.printOrReport(ex :Throwable) {
    if(BuildConfig.DEBUG){
        val sw = StringWriter()
        val pw = PrintWriter(sw, true)
        ex.printStackTrace(pw)
        pw.flush()
        sw.flush()
        Log.e(this::class.java.canonicalName,sw.toString())
    }else {
        MobclickAgent.reportError(this,ex)
    }
}

fun Context.installAPK(filePath: String) {
    val file = File(filePath)
    if (file == null || !file.exists()) {
        Log.e(this::class.java.canonicalName,"installAPK but file is not exists")
        return
    }
    Log.i(this::class.java.canonicalName,"installAPK $filePath")
    val intent = Intent(Intent.ACTION_VIEW)
    //判断是否是AndroidN以及更高的版本
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
        val contentUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".fileProvider", file)
        intent.setDataAndType(contentUri, "application/vnd.android.package-archive")
    } else {
        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive")
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    }
    this.startActivity(intent)
}

fun <T> Context.commonAdapter(layoutId :Int,datas:List<T>,convert:(holder: ViewHolder, data: T, position: Int)->Unit) : CommonAdapter<T> {
  return  object :CommonAdapter<T>(this,layoutId ,datas){
      override fun convert(holder: ViewHolder?, data: T, position: Int) {
          if(holder!=null &&  datas!=null && data!=null && position>=0 && position<datas.size){
              convert(holder,data,position)
          }
      }
  }
}

fun <T> Context.multiItemTypeAdapter(datas:List<T>) : MultiItemTypeAdapter<T> {
    return MultiItemTypeAdapter<T>(this,datas)
}
fun Context.imageOrientation(path:String):Int{
    var orientation=0
    try {
        val cursor = this.getContentResolver().query(Uri.fromFile(File(path)),
                arrayOf<String>(MediaStore.Images.ImageColumns.ORIENTATION), null, null, null)
        if (cursor != null) {
            if (cursor.getCount() !== 1) {
                return -1
            }
            cursor.moveToFirst()
            orientation = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION))
            cursor.close()
        }
    }catch (ex:Throwable){
        this.printOrReport(ex)
    }
    return orientation
}


